package apiday02.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class GradingDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入评委个数：(输入-1退出)");
            int number = scanner.nextInt();
            if (number == -1){
                break;
            }
            if(number<3){
                System.out.println("人数不够3人，请重新输入");
                continue;
            }
            System.out.println("请评委打分：");
            ArrayList<Integer> score = JudgesScore(number);
            System.out.println("你的平均分为："+ AverageScore(score,number));
        }
    }

    //评委打分
    public static ArrayList<Integer> JudgesScore(int num) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(scanner.nextInt());
        }
        return list;
    }

    //算平均分
    public static double AverageScore(ArrayList<Integer> score, int number) {
        int max = score.get(0);
        int min = score.get(0);
        //获取最大最小值
        for (int num : score) {
            if (max < num) {
                max = num;
            }
            if (min > num) {
                min = num;
            }
        }
        //删除最大最小值
        Iterator<Integer> it = score.iterator();
        while (it.hasNext()){
            Integer num = it.next();
            if (num.equals(max)) {
                it.remove();
            }
            if (num.equals(min)) {
                it.remove();
            }
        }
        //算平均分
        double allScore = 0.0;
        for (int num : score) {
            allScore += num;
        }
        return allScore / number;
    }
}
